<template>
  <el-container>
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="">
        <span>电商后台管理系统</span>
      </div>
      <el-button size="mini" type="info" @click="logout()">退出</el-button>
    </el-header>
    <el-container>
      <el-aside  :width="isCollapse?'64px':'200px'">
        <el-menu
          background-color="#151b27"
          text-color="#fff"
          active-text-color="#1a7ee5"
          unique-opened :collapse="isCollapse"
          :collapse-transition="false" router :default-active="activePath">
          <!--水平折叠收起菜单按钮-->
          <div class="toggle-button" @click="toggleCollapse()">|||</div>
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <!--一级菜单-->
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!--二级菜单-->
            <el-menu-item :index="childrenItem.path" v-for="childrenItem in item.children"
                          :key="childrenItem.id" @click="saveActivePath(childrenItem.path)">
              <i class="el-icon-menu"></i>
              <span>{{childrenItem.authName}}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      menuList: [],
      iconObj: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      isCollapse: false,
      activePath: ''
    }
  },
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout () {
      // 清空token
      window.sessionStorage.clear()
      // 跳转到登录界面
      this.$router.push('/login')
    },
    // 获取所有的菜单项
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      // console.log(res)
      this.menuList = res.data
    },
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    saveActivePath (activePath) {
      // console.log(activePath)
      // 将default-active 保存到 sessionStorage中
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;
}
.el-header {
  background-color: #1a2124;
  color: #fff;
  padding-left: 0;
  display: flex;
  justify-content: space-between;  // 贴边对齐
  align-items: center;      // 垂直居中
  > div {
    font-size: 20px;
    display: flex;
    align-items: center;
    > span {
      margin-left: 20px;
    }
  }
}
.el-aside {
  background-color: #151b27;
  > .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #E9EEF3;
  color: #333;
}
.iconfont {
  margin-right: 10px;
}
.toggle-button {
  background-color: #2d3449;
  color: #fff;
  font-size: 14px;
  text-align: center;
  letter-spacing: 0.1em;
  cursor: pointer;
}
</style>
